﻿using System;
using System.Text;
using HslCommunication.Core;
using HslCommunication.Core.Address;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.Fuji
{
	/// <summary>
	/// 富士PLC的SPB协议，详细的地址信息见api文档说明，地址可以携带站号信息，例如：s=2;D100，PLC侧需要配置无BCC计算，包含0D0A结束码<br />
	/// Fuji PLC's SPB protocol. For detailed address information, see the api documentation, 
	/// The address can carry station number information, for example: s=2;D100, PLC side needs to be configured with no BCC calculation, including 0D0A end code
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.Fuji.FujiSPBOverTcp" path="remarks" />
	/// </remarks>
	// Token: 0x020000A5 RID: 165
	public class FujiSPB : SerialDeviceBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.#ctor" />
		// Token: 0x06000C4A RID: 3146 RVA: 0x00047D34 File Offset: 0x00045F34
		public FujiSPB()
		{
			base.ByteTransform = new RegularByteTransform();
			base.WordLength = 1;
			this.LogMsgFormatBinary = false;
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Station" />
		// Token: 0x170001E7 RID: 487
		// (get) Token: 0x06000C4B RID: 3147 RVA: 0x00047D60 File Offset: 0x00045F60
		// (set) Token: 0x06000C4C RID: 3148 RVA: 0x00047D68 File Offset: 0x00045F68
		public byte Station
		{
			get
			{
				return this.station;
			}
			set
			{
				this.station = value;
			}
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000C4D RID: 3149 RVA: 0x00047D74 File Offset: 0x00045F74
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = FujiSPBOverTcp.BuildReadCommand(this.station, address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = FujiSPBOverTcp.CheckResponseData(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<byte[]>(Encoding.ASCII.GetString(operateResult3.Content.RemoveBegin(4)).ToHexBytes());
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000C4E RID: 3150 RVA: 0x00047E18 File Offset: 0x00046018
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<byte[]> operateResult = FujiSPBOverTcp.BuildWriteByteCommand(this.station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = FujiSPBOverTcp.CheckResponseData(operateResult2.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000C4F RID: 3151 RVA: 0x00047E74 File Offset: 0x00046074
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			byte b = (byte)HslHelper.ExtractParameter(ref address, "s", (int)this.station);
			OperateResult<FujiSPBAddress> operateResult = FujiSPBAddress.ParseFrom(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				bool flag2 = address.StartsWith("X") || address.StartsWith("Y") || address.StartsWith("M") || address.StartsWith("L") || address.StartsWith("TC") || address.StartsWith("CC");
				if (flag2)
				{
					bool flag3 = address.IndexOf('.') < 0;
					if (flag3)
					{
						operateResult.Content.BitIndex = (int)(operateResult.Content.Address % 16);
						operateResult.Content.Address = operateResult.Content.Address / 16;
					}
				}
				ushort length2 = (ushort)((operateResult.Content.GetBitIndex() + (int)length - 1) / 16 - operateResult.Content.GetBitIndex() / 16 + 1);
				OperateResult<byte[]> operateResult2 = FujiSPBOverTcp.BuildReadCommand(b, operateResult.Content, length2);
				bool flag4 = !operateResult2.IsSuccess;
				if (flag4)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult<byte[]> operateResult3 = base.ReadBase(operateResult2.Content);
					bool flag5 = !operateResult3.IsSuccess;
					if (flag5)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						OperateResult<byte[]> operateResult4 = FujiSPBOverTcp.CheckResponseData(operateResult3.Content);
						bool flag6 = !operateResult4.IsSuccess;
						if (flag6)
						{
							result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
						}
						else
						{
							result = OperateResult.CreateSuccessResult<bool[]>(Encoding.ASCII.GetString(operateResult4.Content.RemoveBegin(4)).ToHexBytes().ToBoolArray().SelectMiddle(operateResult.Content.BitIndex, (int)length));
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Fuji.FujiSPBOverTcp.Write(System.String,System.Boolean)" />
		// Token: 0x06000C50 RID: 3152 RVA: 0x00048038 File Offset: 0x00046238
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			OperateResult<byte[]> operateResult = FujiSPBOverTcp.BuildWriteBoolCommand(this.station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = FujiSPBOverTcp.CheckResponseData(operateResult2.Content);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000C51 RID: 3153 RVA: 0x00048094 File Offset: 0x00046294
		public override string ToString()
		{
			return string.Format("FujiSPB[{0}:{1}]", base.PortName, base.BaudRate);
		}

		// Token: 0x04000360 RID: 864
		private byte station = 1;
	}
}
